<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@ include file="/common.jsp" %>
<script type="text/javascript">
var dataGrid;
var organizationTree;
$(function() {
    organizationTree = $('#organizationTree').tree({
        url : '${pageContext.request.contextPath }/sys/organization/tree',
        parentField : 'pid',
        lines : true,
        onClick : function(node) {
            dataGrid.datagrid('load', {
                organizationId: node.id
            });
        }
    });
    dataGrid = $('#dataGrid').datagrid({
        url : '${pageContext.request.contextPath }/sys/sysUser/dataGrid',
        fit : true,
        striped : true,
        rownumbers : false,
        pagination : true,
        singleSelect : true,
        idField : 'id',
        sortName : 'id',
        sortOrder : 'asc',
        pageSize : 20,
        pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
        frozenColumns : [[{
	            width : '50',
	            title : 'ID',
	            field : 'id',
	            sortable : true
	        },{
	            width : '80',
	            title : '登录名',
	            field : 'loginName',
	            sortable : true
	        }, {
	            width : '80',
	            title : '姓名',
	            field : 'realName',
	            sortable : true
	        }
        ]],
        columns : [ [ {
            width : '50',
            title : '性别',
            field : 'sex',
            sortable : true,
            formatter : function(value, row, index) {
                switch (value) {
                case 0:
                    return '保密';
                case 1:
                    return '男';
                case 2:
                    return '女';    
                }
            }
        }, {
            width : '50',
            title : '年龄',
            field : 'age',
            sortable : true
        },{
            width : '120',
            title : '电话',
            field : 'phone',
            sortable : true
        }/**,{
            width : '80',
            title : '用户头像',
            field : 'imgurl',
            sortable : true,
            hidden : true,
            formatter : function(value, row, index) {
                return value;
            }
        }*/,{
            width : '60',
            title : '状态',
            field : 'status',
            sortable : true,
            formatter : function(value, row, index) {
                switch (value) {
                case 0:
                    return '正常';
                case 1:
                    return '停用';
                }
            }
        }
        /**,{
            width : '250',
            title : '角色',
            field : 'rolesList',
            sortable : false,
            formatter : function(value, row, index) {
                var roles = [];
                for(var i = 0; i< value.length; i++) {
                    roles.push(value[i].name);  
                }
                return(roles.join('\n'));
            }
        }*/
        ,{
            width : '130',
            title : '创建时间',
            field : 'createTime',
            sortable : true
        },{
            field : 'action',
            title : '操作',
            width : 250,
            formatter : function(value, row, index) {
                var str = '';
                    <shiro:hasPermission  name="sys/sysUser/edit">
                        str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-black\'" onclick="editFun(\'{0}\');" >编辑</a>', row.id);
                    </shiro:hasPermission>  
                    <shiro:hasPermission name="sys/sysUser/delete">
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-black\'" onclick="deleteFun(\'{0}\');" >删除</a>', row.id);
                    </shiro:hasPermission> 
                if(str==''){
                	str = '您尚无[操作权限]';
                }    
                return str;
            }
        }] ],
        onLoadSuccess:function(data){
            $('.user-easyui-linkbutton-edit').linkbutton({text:'编辑',plain:true,iconCls:'fi-pencil icon-black'});
            $('.user-easyui-linkbutton-del').linkbutton({text:'删除',plain:true,iconCls:'fi-x icon-black'});
        },
        toolbar : '#toolbar'
    });
});

function addFun() {
    parent.$.modalDialog({
        title : '添加',
        width : 500,
        height : 300,
        href : '${pageContext.request.contextPath }/sys/sysUser/addPage',
        buttons : [ {
        		text : '取消添加',
            	iconCls:'fi-x icon-black',
            	handler:function(){
            		parent.$.modalDialog.handler.dialog('destroy');
            		parent.$.modalDialog.handler = undefined;
            	}
            },{
            text : '确定添加',
            iconCls:'fi-check icon-black',
            handler : function() {
                parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                var f = parent.$.modalDialog.handler.find('#addForm');
                f.submit();
            }
        } ]
    });
}

function deleteFun(id) {
    if (id == undefined) {//点击右键菜单才会触发这个
        var rows = dataGrid.datagrid('getSelections');
        id = rows[0].id;
    } else {//点击操作里面的删除图标会触发这个
        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
    }
    parent.$.messager.confirm('询问', '您是否要删除当前用户？', function(b) {
        if (b) {
            var currentUserId = '${sessionUser.sysUser.id}';/*当前登录用户的ID*/
            if (currentUserId != id) {
                progressLoad();
                $.post('${pageContext.request.contextPath }/sys/sysUser/delete', {
                    id : id
                }, function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        dataGrid.datagrid('reload');
                    }
                    progressClose();
                }, 'JSON');
            } else {
                parent.$.messager.show({
                    title : '提示',
                    msg : '不可以删除自己！'
                });
            }
        }
    });
}

function editFun(id) {
    if (id == undefined) {
        var rows = dataGrid.datagrid('getSelections');
        id = rows[0].id;
    } else {
        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
    }
    parent.$.modalDialog({
        title : '编辑',
        width : 500,
        height : 300,
        href : '${pageContext.request.contextPath }/sys/sysUser/editPage?id=' + id,
        buttons : [ {
        		text : '取消修改',
            	iconCls:'fi-x icon-black',
            	handler:function(){
            		parent.$.modalDialog.handler.dialog('destroy');
            		parent.$.modalDialog.handler = undefined;
            	}
            },{
            text : '确定修改',
            iconCls:'fi-check icon-black',
            handler : function() {
                parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                var f = parent.$.modalDialog.handler.find('#editForm');
                f.submit();
            }
        } ]
    });
}

//用户角色授权
function grantRoleFun(id) {
  	try{
		if (id == undefined) {
	        var rows = dataGrid.datagrid('getSelections');
	        id = rows[0].id;
	    } else {
	        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	    }
	}catch(e){
		if(id == undefined){
	    	parent.$.messager.alert('提示', '请选择一个需要授权的用户！', 'info');
	    	return ;
	    }
	}
    
    parent.$.modalDialog({
        title : '角色授权',
        width : 500,
        height : 500,
        href : '${pageContext.request.contextPath }/sys/sysUser/grantRolePage?id=' + id,
        buttons : [ {
        	text : '取消授权',
        	iconCls:'fi-x icon-black',
        	handler:function(){
        		parent.$.modalDialog.handler.dialog("destroy");
        		parent.$.modalDialog.handler = undefined ;
        	}
        },{
            text : '确定授权',
            iconCls: 'fi-check icon-black',
            handler : function() {
                parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                var f = parent.$.modalDialog.handler.find('#grantRoleForm');
                f.submit();
            }
        } ]
    });
}
    
    
function searchFun() {
    dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
}
function cleanFun() {
    $('#searchForm input').val('');
    dataGrid.datagrid('load', {});
}
</script>
<body class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'north',border:false" style="height: 30px; overflow: hidden;background-color: #fff">
        <form id="searchForm">
            <table>
                <tr>
                    <th>姓名:</th>
                    <td><input name="name" placeholder="请输入用户姓名"/></td>
                    <th>创建时间:</th>
                    <td>
                    	<input name="createTimeStart" placeholder="点击选择时间" 
                    		onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />
                                                                        至<input  name="createTimeEnd" placeholder="点击选择时间" 
                            onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />
                    	<a href="javascript:void(0);" class="easyui-linkbutton" 
                    		data-options="iconCls:'fi-magnifying-glass icon-black',plain:true" onclick="searchFun();">查询</a>
                    	<a href="javascript:void(0);" class="easyui-linkbutton" 
                    		data-options="iconCls:'fi-x icon-black',plain:true" onclick="cleanFun();">清空</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',border:true,title:'用户列表'" >
        <table id="dataGrid" data-options="fit:true,border:false"></table>
    </div>
    <div data-options="region:'west',border:true,split:false,title:'部门列表'"  style="width:150px;overflow: hidden; ">
        <ul id="organizationTree"  style="width:160px;margin: 10px 10px 10px 10px">
        </ul>
    </div> 
    <div id="toolbar" style="display: none;">
        <shiro:hasPermission name="sys/sysUser/add">
            <a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-black'">添加</a>
        </shiro:hasPermission>  
        <shiro:hasPermission name="sys/sysUser/grantRole">
            <a onclick="grantRoleFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-torsos-all icon-black'">用户授权</a>
        </shiro:hasPermission>  
    </div>
</body>
</html>
